package com.xianbeiying.app.ckgl.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.xianbeiying.app.ckgl.MyApplication;
import com.xianbeiying.app.ckgl.R;
import com.xianbeiying.app.ckgl.adapter.MyCollectListdapter;
import com.xianbeiying.app.ckgl.bean.MyCollectBean;
import com.xianbeiying.app.ckgl.bean.MyCollectDeleteBean;
import com.xianbeiying.app.ckgl.http.HttpCallBackUtil;
import com.xianbeiying.app.ckgl.model.DeleteModel;
import com.xianbeiying.app.ckgl.model.GetModel;
import com.xianbeiying.app.ckgl.util.ToastUtil;
import com.xianbeiying.app.ckgl.widght.popwindow.CenterPopWindow;

/**
 * 我的收藏
 * */
public class MyCollectActivity extends BaseActivity  implements HttpCallBackUtil,View.OnClickListener{
    private ImageView iv_title_left;
    private TextView tv_title;
    private ListView mycollect_list;
    private MyCollectListdapter mycollectAdapter;
    private GetModel getModel;
    private DeleteModel deleteModel;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_collect);
        initView();
    }
    private void initView() {
        MainActivity.fragmentid=3;
        getModel=new GetModel(this);
        getModel.addResponseListener(this);
        deleteModel=new DeleteModel(this);
        deleteModel.addResponseListener(this);
        tv_title=(TextView) findViewById(R.id.tv_title);
        iv_title_left=(ImageView) findViewById(R.id.iv_title_left);
        mycollect_list=(ListView) findViewById(R.id.mycollect_list);
        initData();
        initEvent();
    }
    private void initData() {
        tv_title.setText("我的收藏");
        getModel.GETCOLLECT("GET_COLLECTIONS", MyApplication.loginBean.getUSERID(),true);
    }
    private CenterPopWindow centerPopWindow;
    private int selectId;
    private void initEvent(){
        iv_title_left.setOnClickListener(this);
        mycollect_list.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                     startActivity(new Intent().setClass(MyCollectActivity.this,MyCollectDetailActivity.class).putExtra("collectBean",myCollectBean.getV().getCOLLECTIONS().get(position)));
            }
        });
        //长按删除
        mycollect_list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                //进行删除
                centerPopWindow = new CenterPopWindow(MyCollectActivity.this, R.layout.pop_window_delete);
                centerPopWindow.setText(R.id.tv_title,"确定删除");
                centerPopWindow.setTextOnClickListener(R.id.tv_delete,MyCollectActivity.this);
                centerPopWindow.setTextOnClickListener(R.id.tv_cancel, MyCollectActivity.this);
                centerPopWindow.setTextOnClickListener(R.id.ly_exit, MyCollectActivity.this);
                selectId=position;
                return true;
            }
        });
    }
    private MyCollectBean myCollectBean;
    private MyCollectDeleteBean myCollectDeleteBean;
    @Override
    public void onCallBackData(String url, Object content) {
         if(url.equals("我的收藏")){
             if(content!=null){
                 myCollectBean= JSON.parseObject(new String((byte []) content),MyCollectBean.class);
                 if(myCollectBean.isR()){
                     if(myCollectBean.getV().getCOLLECTIONS().size()!=0){
                         mycollectAdapter=new MyCollectListdapter(this,myCollectBean);
                         mycollect_list.setAdapter(mycollectAdapter);
                     }else{
                         mycollect_list.setAdapter(null);
                     }
                 }
             }
         }
        if(url.equals("删除收藏")){
            myCollectDeleteBean= JSON.parseObject(new String((byte []) content),MyCollectDeleteBean.class);
            if(myCollectDeleteBean.isR()){
                ToastUtil.showInfo(MyCollectActivity.this,"删除成功");
                getModel.GETCOLLECT("GET_COLLECTIONS", MyApplication.loginBean.getUSERID(),true);
                centerPopWindow.dismiss();
            }else{
                ToastUtil.showInfo(MyCollectActivity.this,myCollectDeleteBean.getERR_MSG());
                centerPopWindow.dismiss();
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_title_left:
                finish();
                break;
            //删除
            case R.id.tv_delete:
                deleteModel.DeleteCollect("DELETE_COLLECTION",myCollectBean.getV().getCOLLECTIONS().get(selectId).getID(),true);
                break;
            //取消
            case R.id.tv_cancel:
            case R.id.ly_exit:
                centerPopWindow.dismiss();
                break;
        }
    }
}
